package com.qf.flashlib.app;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.TextView;
import android.widget.Toast;

import com.qf.flashlib.R;
import com.qf.flashlib.sharedpreferences.SharePrefConstants;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * OamLib
 * 2018/9/12 11:07
 */
public class FlashLib
{
    private static FlashLib mAppLib;

    private Application mApplication;
    private boolean mIsInitialized = false;
    private SharedPreferences mSharePref = null;
    private SharedPreferences mSharePrefSave = null;
    private Toast mToast;
    private int mToastY = 0;// toast默认显示高度

    /**
     * 屏幕密度，默认1.5
     */
    public static Float density = 1.5F;
    /**
     * 屏幕宽
     */
    public static int width = 0;
    /**
     * 屏幕高
     */
    public static int height = 0;

    public static FlashLib getInstance()
    {
        return mAppLib;
    }

    /**
     * 创建一个可缓存线程池，如果线程池长度超过处理需要，可灵活回收空闲线程，若无可回收，则新建线程。
     */
    public static ExecutorService threadPoolExecutor = Executors.newCachedThreadPool();

    public static void onCreate(Application app)
    {
        mAppLib = new FlashLib(app);
    }

    private FlashLib(Application app)
    {
        mApplication = app;
    }

    public Context getContext()
    {
        return mApplication;
    }

    public boolean isInitialized()
    {
        return mIsInitialized;
    }

    /**
     * 会随着账号退出而清空的SharedPreferences实例
     */
    public SharedPreferences getPreferences()
    {
        if (mSharePref == null)
        {
            int mode = mApplication.MODE_PRIVATE;
            mSharePref = mApplication.getSharedPreferences(
                    SharePrefConstants.SHARED_PREFERENCE_NAME_CLEAR, mode);
        }
        return mSharePref;
    }

    /**
     * 不可清空的SharedPreferences实例
     */
    public SharedPreferences getPreferencesSave()
    {
        if (mSharePrefSave == null)
        {
            int mode = mApplication.MODE_PRIVATE;
            mSharePrefSave = mApplication.getSharedPreferences(
                    SharePrefConstants.SHARED_PREFERENCE_NAME_SAVE, mode);
        }
        return mSharePrefSave;
    }

    public void resetPreference()
    {
        mSharePref = null;
    }

    public String getDataString(String key)
    {
        SharedPreferences settings = getPreferences();
        return settings.getString(key, "");
    }

    public String getDataString(String key, String def)
    {
        SharedPreferences settings = getPreferences();
        return settings.getString(key, def);
    }

    public int getDataInt(String key)
    {
        SharedPreferences settings = getPreferences();
        return settings.getInt(key, 0);
    }

    public int getDataInt(String key, int value)
    {
        SharedPreferences settings = getPreferences();
        return settings.getInt(key, value);
    }

    public boolean getDataBoolean(String key, boolean defaltBol)
    {
        SharedPreferences settings = getPreferences();
        return settings.getBoolean(key, defaltBol);
    }

    public long getDataLong(String key)
    {
        SharedPreferences settings = getPreferences();
        return settings.getLong(key, 0);
    }

    public void setData(String key, String value)
    {
        SharedPreferences settings = getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public void setData(String key, int value)
    {
        SharedPreferences settings = getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public void setData(String key, boolean value)
    {
        SharedPreferences settings = getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public void setData(String key, long value)
    {
        SharedPreferences settings = getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.putLong(key, value);
        editor.commit();
    }

    /**
     * 清空所有数据
     */
    public void clearAllData()
    {
        SharedPreferences settings = getPreferences();
        SharedPreferences.Editor editor = settings.edit();
        editor.clear();
        editor.commit();
    }

    /**
     * toast 文字
     */
    public static void showToast(int textResId)
    {
        showToast(mAppLib.getContext().getString(textResId), 0);
    }

    /**
     * toast 文字带图片
     */
    public static void showToast(int textResId, int iconResId)
    {
        showToast(mAppLib.getContext().getString(textResId), iconResId);
    }

    /**
     * toast 文字
     */
    public static void showToast(String text)
    {
        showToast(text, 0);
    }

    /**
     * toast 文字
     */
    public static void showToastByDuration(String text, int duration)
    {
        showToast(text, 0, duration);
    }

    /**
     * toast 文字带图片
     */
    public static void showToast(String text, int iconResId)
    {
        if (TextUtils.isEmpty(text))
        {
            return;
        }
        TextView tv;
        if (null == mAppLib.mToast)
        {
            mAppLib.mToast = Toast.makeText(mAppLib.getContext(), text, Toast.LENGTH_SHORT);
            LayoutInflater inflate = (LayoutInflater) mAppLib.getContext().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            tv = (TextView) inflate.inflate(R.layout.dialog_toast, null);
            mAppLib.mToast.setView(tv);
            mAppLib.mToastY = mAppLib.mToast.getYOffset();
        }
        tv = (TextView) mAppLib.mToast.getView();
        tv.setText(text);
        Drawable icon = null;
        if (iconResId > 0)
        {
            try
            {
                icon = mAppLib.getContext().getResources().getDrawable(iconResId);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            mAppLib.mToast.setGravity(Gravity.CENTER, 0, 0);
        }
        else
        {
            mAppLib.mToast.setGravity(Gravity.BOTTOM, 0, mAppLib.mToastY);
        }
        tv.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);
        mAppLib.mToast.show();
    }

    /**
     * toast 文字带图片 自定义提示时长
     */
    public static void showToast(String text, int iconResId, int duration)
    {
        if (TextUtils.isEmpty(text))
        {
            return;
        }
        TextView tv;
        if (null == mAppLib.mToast)
        {
            mAppLib.mToast = Toast.makeText(mAppLib.getContext(), text, duration);
            LayoutInflater inflate = (LayoutInflater) mAppLib.getContext().getSystemService(
                    Context.LAYOUT_INFLATER_SERVICE);
            tv = (TextView) inflate.inflate(R.layout.dialog_toast, null);
            mAppLib.mToast.setView(tv);
            mAppLib.mToastY = mAppLib.mToast.getYOffset();
        }
        tv = (TextView) mAppLib.mToast.getView();
        tv.setText(text);
        Drawable icon = null;
        if (iconResId > 0)
        {
            try
            {
                icon = mAppLib.getContext().getResources().getDrawable(iconResId);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            mAppLib.mToast.setGravity(Gravity.CENTER, 0, 0);
        }
        else
        {
            mAppLib.mToast.setGravity(Gravity.BOTTOM, 0, mAppLib.mToastY);
        }
        tv.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);
        mAppLib.mToast.show();
    }

    /**
     * 取消toast
     */
    public static void cancelToast()
    {
        if (null != mAppLib.mToast)
        {
            mAppLib.mToast.cancel();
        }
    }

}
